package com.sanpu.kxe.business.main.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.bean.CertificationOldOutput;
import com.sanpu.kxe.business.main.contract.MainConfig;
import com.sanpu.kxe.business.main.fragment.OldAuthSecondStepFragment;
import com.sanpu.kxe.business.main.fragment.OldAuthThirdStepFragment;
import com.sanpu.kxe.business.main.interfaces.IOnSwitchPageListener;
import com.sanpu.kxe.common.custom.SerializableHashMap;
import com.sanpu.kxe.common.interfaces.DialogViewListener;
import com.sanpu.kxe.jump.eventbus.ReviewPhotoEvent;
import com.sanpu.kxe.utils.DialogUtil;
import com.sanpu.kxe.utils.FileUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.HashMap;
import java.util.List;

/**
 * Description：认证人工审核第二步
 * Created by 薛勇军 on 2019/5/15.
 */

public class OldCertificateSecondActivity extends BaseTitleActivity {

    private OldAuthSecondStepFragment secondFragment;
    private OldAuthThirdStepFragment thirdStepFragment;

    private int mCurrentStep = 1;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_certificate_second;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("实名认证");
        secondFragment = new OldAuthSecondStepFragment();
        thirdStepFragment = new OldAuthThirdStepFragment();
        if (savedInstanceState != null) {
            List<Fragment> fragmentList = getSupportFragmentManager().getFragments();
            for (Fragment fragment : fragmentList) {
                if (fragment instanceof OldAuthSecondStepFragment) {
                    secondFragment = (OldAuthSecondStepFragment) fragment;
                } else if (fragment instanceof  OldAuthThirdStepFragment) {
                    thirdStepFragment = (OldAuthThirdStepFragment) fragment;
                }
            }
            if (thirdStepFragment == null) {
                getSupportFragmentManager().beginTransaction().show(secondFragment).commit();
            } else {
                getSupportFragmentManager().beginTransaction().show(secondFragment).hide(thirdStepFragment).commit();
            }
        } else {  // 正常时
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.add(R.id.container, secondFragment);
            ft.commit();
        }
    }

    @Override
    protected void initData() {
        super.initData();
        boolean isComeFromNews = getIntent().getBooleanExtra(MainConfig.INTENT_COME_FROM_NEWS, false);
        if (isComeFromNews) {
            Bundle fileBundle = getIntent().getExtras();
            if (fileBundle.containsKey(MainConfig.INTENT_CERTIFICATE_FILE)) {
                SerializableHashMap serializableHashMap = (SerializableHashMap) fileBundle.get(MainConfig.INTENT_CERTIFICATE_FILE);
                if (serializableHashMap != null) {
                    mUploadFiles = serializableHashMap.getMap();
                }
            }
        }
        CertificationOldOutput mOutput = (CertificationOldOutput)
                getIntent().getSerializableExtra(MainConfig.INTENT_CERTIFICATION_OLD_INPUT);
        Bundle bundle = new Bundle();
        bundle.putSerializable(MainConfig.INTENT_CERTIFICATION_OLD_INPUT, mOutput);
        bundle.putBoolean(MainConfig.INTENT_COME_FROM_NEWS, isComeFromNews);
        secondFragment.setArguments(bundle);
        thirdStepFragment.setArguments(bundle);
    }

    @Override
    protected void setListener() {
        super.setListener();
        commonTitle.setLeftOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        secondFragment.setOnSwitchPageListener(new IOnSwitchPageListener() {
            @Override
            public void onSwitchPage() {
                try {
                    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                    ft.setCustomAnimations(
                            R.anim.in_from_right, R.anim.in_to_left,
                            R.anim.out_to_left, R.anim.out_from_right);
                    if (thirdStepFragment != null && thirdStepFragment.isAdded()) {
                        ft.show(thirdStepFragment).hide(secondFragment);
                    } else {
                        ft.add(R.id.container, thirdStepFragment).hide(secondFragment);
                    }
                    ft.commit();
                    mCurrentStep = 2;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (thirdStepFragment.isVisible()) {
            switchPageSecond();
        } else {
            DialogUtil.getInstance().dialogExistTitleTwoBtnHaveCallBack(mContext, "温馨提示",
                    "是否放弃这次认证操作", "取消", "放弃",
                    new DialogViewListener() {
                        @Override
                        public void btnRightConfirm() {
                            finish();
                        }

                        @Override
                        public void btnLeftCancel() {

                        }
                    });
        }
    }

    /**
     * 返回到认证第二步
     */
    public void switchPageSecond() {
        try {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.setCustomAnimations(
                    R.anim.out_to_left, R.anim.out_from_right,
                    R.anim.in_from_right, R.anim.in_to_left
            );
            ft.hide(thirdStepFragment);
            ft.show(secondFragment);
            ft.commit();
            mCurrentStep = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(ReviewPhotoEvent event) {
        if (event != null) {
            switch (mCurrentStep) {
                case 1:
                    secondFragment.refreshView(event);
                    break;
                case 2:
                    thirdStepFragment.refreshView(event);
                    break;
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (mCurrentStep) {
            case 1:
                secondFragment.onActivityResult(requestCode, resultCode, data);
                break;
            case 2:
                thirdStepFragment.onActivityResult(requestCode, resultCode, data);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FileUtil.deleteCache();
    }

    private HashMap<String, File> mUploadFiles = new HashMap<>();

    public void setUploadFiles(HashMap<String, File> mUploadFiles) {
        this.mUploadFiles = mUploadFiles;
    }

    public HashMap<String, File> getmUploadFiles() {
        return mUploadFiles;
    }
}
